package  
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
    import flash.events.*;
    import flash.net.*;
	import flash.system.*
	import flash.external.ExternalInterface;

	/**
	 * ...
	 * @author egoing
	 */
	public class HttpRequest extends MovieClip
	{
		private var requests:Array;
		public function HttpRequest() 
		{
			Security.allowDomain('*');
			requests = new Array();
			try {
				if (ExternalInterface.available) {
					ExternalInterface.addCallback('ajax', ajax);
				}
			} catch (e:Error) {
				trace(e);
			}
			
		}
		private function ajax(obj) {
			var url:URLVariables = new URLVariables();
			if (obj.data != undefined) {
				var _data = obj.data.split('&');
				for (var i:Number; i < _data.length; i++) {
					var __data = _data[i].split('=');
					url.data[__data[0]] = __data[1];
				}
			}
			trace(decodeURIComponent(obj.url));
			var request:URLRequest = new URLRequest(decodeURIComponent(obj.url));
			var loader:URLLoader = new URLLoader();
			loader.data = url;
			loader.dataFormat = 'text';
			configureListeners(loader);
			try {
				loader.load(request);
				requests.push([loader,obj]);
			} catch (error:Error) {
				trace("Unable to load requested document.");
			}
		}
		
		 private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }

        private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
			for (var name:String in requests) {
				if (event.target == requests[name][0]) {
					if (requests[name][1].callback != undefined) {
						var obj = requests[name][1];
						obj.data = loader.data;
						trace(obj.callback,obj.id,obj);
						ExternalInterface.call(obj.callback,obj.id,obj);
						return;
					}
				}
			}
        }

        private function openHandler(event:Event):void {
            //trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            //trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            //trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            //trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            //trace("ioErrorHandler: " + event);
        }
	}
	
}